import { WeatherData, HourlyForecast, DailyForecast } from '@/types/weather';

class WeatherService {
    private baseUrl = process.env.NEXT_PUBLIC_WEATHER_API_URL;
    private apiKey = process.env.NEXT_PUBLIC_WEATHER_API_KEY;

    async getCurrentWeather(lat: number, lon: number): Promise<WeatherData> {
        try {
            const response = await fetch(
                `${this.baseUrl}/current?lat=${lat}&lon=${lon}&appid=${this.apiKey}`
            );
            return await response.json();
        } catch (error) {
            console.error('Failed to fetch current weather:', error);
            throw error;
        }
    }

    async getForecast(lat: number, lon: number): Promise<{
        hourly: HourlyForecast[];
        daily: DailyForecast[];
    }> {
        try {
            const response = await fetch(
                `${this.baseUrl}/forecast?lat=${lat}&lon=${lon}&appid=${this.apiKey}`
            );
            return await response.json();
        } catch (error) {
            console.error('Failed to fetch forecast:', error);
            throw error;
        }
    }
}

export const weatherService = new WeatherService();